<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/token.jsp"%>
<%@ include file="/common/operatehistory.jsp"%>
<%@ include file="/common/code.jsp"%>
<%@ include file="/common/user.jsp"%>
<%@ include file="/common/message.jsp"%>
<%
	// 关注病人
	String code = input.getDesc("code", "").trim();
	Integer selfId = token.getUid();
	OperateHistory oh = OperateHistory.fromMap(parameters, OperateHistory.class);
	// 扫描二维码形式
	if (!code.equals("")) {
		Message codeMsg = decodeQrCode(code);
		if (codeMsg.getErrcode() != 0) {
			Tools.encryptPrint(out, codeMsg);
			return;
		}
		Map codeMap = (Map)codeMsg.get("code");
		String type = Tools.getDesc(codeMap.get("type"));
		if (!type.equals("patient")) {
			Tools.encryptPrint(out, new Message(30041, "请扫描患者二维码"));
			return;
		}
		String id = Tools.getDesc(codeMap.get("id"));
		oh.setOid(id);
	}
	User self = userByUid(selfId);
	if (self == null || Tools.getDesc(self.getJson("role"), "1").equals("2")) {
		Tools.encryptPrint(out, new Message(30031, "您不能关注病人"));
		return;
	}
	// 如果是医生主动关注患者，则患者自动关注该医生
	if (USER_EMPLOYEE.equals(self.getType())) {
		UserProfile up = selfProfileByUid(Tools.getInt(Tools.getInt(oh.getOid())));
		if ( up == null || isEmpty(up.getName())  || isEmpty(up.getCardNumber())) {
			msg = new Message(30041, "患者未实名认证，暂不可被关注");
			Tools.encryptPrint(out, msg);
			return;
		}
		OperateHistory ohSectionDoctor = OperateHistory.fromMap(parameters, OperateHistory.class);
		ohSectionDoctor.setOid(selfId + "");
		ohSectionDoctor.setCategory(SECTION_DOCTOR);
		ohSectionDoctor.setOperate(ATTENTION);
		ohSectionDoctor.setUid(oh.getOid());
		Message pMsg = operate(ohSectionDoctor, false);
		if (pMsg.getErrcode() == 0) {
			User commentUser = userByUid(selfId);
			String tips = String.format("医生%s特别关注了您", new Object[]{getDisplayName(commentUser)});
			createMsg(Tools.getInt(oh.getOid()), oh.getHospital(), "医生关注提醒", "系统提醒", tips, DOCTORATTENTION, selfId + "");
		}
	}
	oh.setUid(selfId + "");
	oh.setCategory(SECTION_PATIENT);
	oh.setOperate(ATTENTION);
	Tools.encryptPrint(out, operate(oh, false));
%>
